home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / minix / libsrc~1.z / libsrc~1 / execlp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-12-28  |  2.0 KB  |  102 lines

  1. #define __SRC__
  2. #include "lib.h"
  3.  
  4. /* execlp(3) and execvp(3)
  5.  *
  6.  * Author: Terrence W. Holm      July 1988
  7.  */
  8.  
  9. /*
  10.  *  Execlp(3) and execvp(3) are like execl(3) and execv(3),
  11.  *  except that they use the environment variable $PATH as
  12.  *  a search list of possible locations for the executable
  13.  *  file, if <file> does not start with a '/'.
  14.  *
  15.  *  The path search list is a list of directory names separated
  16.  *  by ':'s. If a colon appears at the beginning or end of the
  17.  *  list, or two appear together, then an empty prefix is
  18.  *  tried. If $PATH is not in the environment, it defaults to
  19.  *  ":/bin:/usr/bin".
  20.  *
  21.  *  For example, if <file> is "sh", and the $PATH is
  22.  *  ":/bin:/usr/local:/usr/bin", then the attempts will be:
  23.  *  "sh", "/bin/sh", "/usr/local/sh" and "/usr/bin/sh".
  24.  *
  25.  *  If the <file> is not an executable file in one of the
  26.  *  directories, then -1 is returned.
  27.  */
  28.  
  29. #include <errno.h>
  30. #include <string.h>
  31. #include <sys/types.h>
  32. #include <unistd.h>
  33.  
  34. #ifndef X_OK
  35. #define X_OK 1
  36. #endif
  37.  
  38. #ifndef NULL
  39. #define  NULL  (char *) 0
  40. #endif
  41.  
  42. #ifndef __STDC__
  43. extern char *index();
  44. extern char *getenv();
  45. #else
  46. char *index(_CONST char *, int);
  47. char *getenv(_CONST char *);
  48. #endif
  49.  
  50. extern char **environ;
  51. extern int    errno;
  52.  
  53.  
  54. int execlp( file, arg0 )
  55.   _CONST char *file;
  56.   _CONST char *arg0;
  57.  
  58.   {
  59.   return( execvp( file, &arg0 ) );
  60.   }
  61.  
  62.  
  63. int execvp( file, argv )
  64.   _CONST char *file;
  65.   _CONST char *argv[];
  66.  
  67.   {
  68.   char path_name[100];
  69.   char *next;
  70.   char *path = getenv( "PATH" );
  71.  
  72.   if ( path == NULL )
  73.     path = ":/bin:/usr/bin";
  74.  
  75.   if ( file[0] == '/' )
  76.     path = "";
  77.  
  78.   do  {
  79.       next = index( path, ':' );
  80.  
  81.       if ( next == NULL )
  82.       strcpy( path_name, path );
  83.       else
  84.       {
  85.       *path_name = '\0';
  86.       strncat( path_name, path, next - path );
  87.       path = next + 1;
  88.       }
  89.  
  90.       if ( *path_name != '\0' )
  91.           strcat( path_name, "/" );
  92.  
  93.       strcat( path_name, file );
  94.  
  95.       if ( access( path_name, X_OK ) == 0 )
  96.       execve( path_name, argv, environ );
  97.       } while ( next != NULL );
  98.  
  99.   errno = ENOENT;
  100.   return( -1 );
  101.   }
  102.